using Microsoft.Practices.Unity;

namespace UniAspect.ContainerOverride
{
    /// <summary>
    /// Extension that sets overrides policies list
    /// </summary>
    public class OverridingExtension : UnityContainerExtension
    {
        public OverridingExtension AddOverride(IUnityContainer container, string overrideName)
        {
            AddOverride(container, new ContainerOverrideKey(overrideName));
            return this;
        }

        public OverridingExtension AddOverride(IUnityContainer container, ContainerOverrideKey overrideKey)
        {
            Context.Policies.Set<IContainerOverridePolicy>(
                    new ContainerOverridePolicy(container),
                    overrideKey);
            return this;
        }

        public IUnityContainer GetOverride(string overrideName)
        {
            var policy = Context.Policies.Get<IContainerOverridePolicy>(new ContainerOverrideKey(overrideName));
            if(policy == null)
                return null;
            return policy.Override;
        }

        protected override void Initialize()
        {
        }
    }
}